<?php 
	include "include.php";
	
	$isSecureAccess = SecurityManager::getUser() != null;
	
	if( $isSecureAccess ) {
		$userService = new UserService();
		$users = $userService->listActiveUsers();
		$user = new UserModel();
		$user = SecurityManager::getUser();
		
		$save = Scope::loadVar("action") == "save";
		
		if( $save ) {
			$id= Scope::loadVar("user_id");
			$myId = $user->getId();
			$subject = Scope::loadVar("subject");
			$message = Scope::loadVar("message");
			$date = date("Y-m-d H:i:s");
			$sql = "insert into t_message (date, message, subject, user_id, isread, isalert, sendbysite) values ('$date','$message', '$subject', $id, 1, 0, 1)";
			
			$conexao = mysql_pconnect(Config::$db_host,Config::$db_user,Config::$db_password) or die($msg[0]);
			
			if (!$conexao) {
  				die('Could not connect: ' . mysql_error());
  			}
			
			mysql_select_db("cbgrid9",$conexao) or die($msg[1]);
			
			mysql_query($sql);
			
			mysql_close($conexao);
			echo"<center>SMS enviado com sucesso!</center>";
		} else {		
?>	

<div id="sms">
	<fieldset>
		<legend>:: Envio de SMS</legend>
		<form method="POST" action="?option=sms&action=save">
			<p>
				<select name="user_id" >
					<option>Selecione o destino do SMS</option>
<?php 
					print_r($users);
					foreach( $users as $user ) {
?>
					<option value="<?php echo $user->getId(); ?>"><?php echo $user->getName(); ?></option>					
<?php 				
					}
?>		
				</select>
			</p>
			<p>
				<input type="text" name="subject" />
			</p>
			<p>
				<textarea rows="2" cols="22" name="message"></textarea>
			</p>
			<p>
				<input type="submit" value="enviar">
			</p>
		</form>
	</fieldset>
</div>
<?php	}	
	} else {
		MessageManager::setError("Voc&ecirc; n&atilde;o tem permiss&atilde;o para acessar esta p&aacute;gina");
		Util::includeFile("error.php",true);
	}
?>